<%--
  Created by IntelliJ IDEA.
  User: SamZhao
  Date: 2016/12/6
  Time: 19:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ include file="/WEB-INF/jsp/include.inc.jsp"%>
<script src="/js/jquery-1.8.3.min.js" ></script>

<!-- BEGIN MAIN CONTENT -->
<section id="main-content">
    <!-- BEGIN WRAPPER  -->
    <section class="wrapper">
        <!-- BEGIN ROW  -->
        <div class="row">
            <div class="col-lg-12" style="width: 800px;">
                <section class="panel">
                    <header class="panel-heading">
                        <span class="label label-primary">科室变更</span>
                        <span class="tools pull-right">
                           <a href="javascript:;" class="fa fa-chevron-down"></a>
                           <a href="javascript:;" class="fa fa-times"></a>
                           </span>
                    </header>
                    <div class="panel-body">
                        <div class="modal fade" id="myModal3" >
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        <h4 class="modal-title">错误!</h4>
                                    </div>
                                    <div class="modal-body" id="erroText">
                                    </div>
                                    <div class="modal-footer">
                                        <button class="btn btn-danger" type="button" data-dismiss="modal"> Ok</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal fade" id="myModal"  >
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        <h4 class="modal-title">角色变更</h4>
                                    </div>
                                    <div class="modal-body">
                                        提交成功!
                                    </div>
                                    <div class="modal-footer">
                                        <button class="btn btn-success" type="button" data-dismiss="modal">确定</button>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class=" form">
                            <form class="cmxform form-horizontal tasi-form" id="commentForm" method="get" action="/role/saveRole.html">
                                <div class="form-group ">
                                    <label for="name" class="control-label col-lg-2">科室</label>
                                    <div class="col-lg-10">
                                        <input class=" form-control" id="name" name="name"  type="text" required value="${department.name}"/>
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="number" class="control-label col-lg-2">编号</label>
                                    <div class="col-lg-10">
                                        <input class=" form-control" id="number" name="number"  type="text" required value="${department.number}"/>
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="departmentType" class="control-label col-lg-2">所属类别</label>
                                    <div class="col-lg-10">
                                        <select class="form-control m-bot15" id="departmentType">
                                            <c:forEach items="${typelist }" var="item">
                                                <option value="${item.id}" <c:if test="${item.id == department.typeid}">selected</c:if>>${item.name}</option>
                                            </c:forEach>
                                        </select>
                                    </div>
                                </div>
                                <input type="hidden" name="typeid" id="typeid" value="${department.typeid}"/>
                                <input id="id" name="id" value="${department.id}" type="hidden"/>

                                <div class="form-group">
                                    <div class="col-lg-offset-2 col-lg-10">
                                        <button class="btn btn-primary" type="button" id="saveBtn">保存</button>
                                        <button class="btn btn-default" type="button" onclick="cancelBtn();">取消</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </section>
            </div>
        </div>
        <!-- END ROW  -->
    </section>
</section>
<script type="text/javascript">
    function cancelBtn(){
        window.location = "/department/list.html";
    }
    $(function(){
        $("#myModal3").modal("hide");
        $("#myModal").modal("hide");
        // save
        $("#saveBtn").on('click', function() {
            $("#typeid").val($('#departmentType option:selected').val());
            if($("#roleMemo").val()==""){
                $("#erroText").html("角色不能为空!");
                $("#myModal3").modal("show");
            }else if($("#number").val()==""){
                $("#erroText").html("编号不能为空!");
                $("#myModal3").modal("show");
            }else if($("#typeid").val()==""){
                $("#erroText").html("类别不能为空!");
                $("#myModal3").modal("show");
            }
            else{
                var flag = checkRole();
                if(flag == false ){
                    $.ajax({
                        type: "POST",
                        url: "/department/saveDepartment.html",
                        data:$('#commentForm').serialize(),// 你的formid
                        async: false,
                        error: function() {
                            $("#erroText").html("网络异常!");
                            $("#myModal3").modal("show");
                        },
                        success: function(data) {

                            if(data == 'success'){
                                $("#myModal").modal("show");

                                window.setInterval(function()//延迟进行跳转
                                {
                                    intervalRun();
                                }, 1000);
                            }else{
                                $("#erroText").html("新增出错!");
                                $("#myModal3").modal("show");
                            }

                        }
                    });
                }else{
                    $("#erroText").html("该科室已存在!");
                    $("#myModal3").modal("show");
                }
            }
        });

        function intervalRun(){
            $("#myModal").modal("hide");
            window.location = "/department/list.html";
        }

        function checkRole(){
            var flag = false;
            $.ajax({
                type: "POST",
                url: "/department/checkDepartment.html",
                data:$('#commentForm').serialize(),// 你的formid
                async: false,
                error: function() {
                    $("#erroText").html("网络异常!");
                    $("#myModal3").modal("show");
                },
                success: function(data) {
                    if(data == 'success')
                        flag = true;
                }
            });
            return flag;
        }

    });

</script>
